package com.atguigu.ggkt.vod.service.impl;

import com.atguigu.ggkt.vod.service.VodService;
import com.atguigu.ggkt.model.vod.Video;

import com.atguigu.ggkt.vod.mapper.VideoMapper;
import com.atguigu.ggkt.vod.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author sleephardzz
 * @since 2023-07-10
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    //删除小节
    @Override
    public void removeVideoByCourseId(Long id) {
        //并且把内部视频删掉：
        LambdaQueryWrapper<Video> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Video::getCourseId, id);
        //根据课程id获得所有小节
        List<Video> videoList = baseMapper.selectList(wrapper);
        //遍历所有小节，获得视频id
        for (Video video : videoList) {
            String videoSourceId = video.getVideoSourceId();
            if (!StringUtils.isEmpty(video)) {
                //视频id不为空，则删除腾讯云视频id
                //调用刚刚的方法：
                vodService.removeVideo(videoSourceId);
            }
        }
//        LambdaQueryWrapper<Video> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Video::getCourseId, id);
        baseMapper.delete(wrapper);
    }

    @Autowired
    private VodService vodService;

    //删除小节里面的视频，同时删除小节
    @Override
    public void removeVideoById(Long id) {
        //id查询小节
        Video video = baseMapper.selectById(id);
        String videoSourceId = video.getVideoSourceId();
        //判断视频id是否为空
        if (!StringUtils.isEmpty(videoSourceId)) {
            /** 根据id删除视频 */
            vodService.removeVideo(videoSourceId);
        }
        //根据id删除小节
        baseMapper.deleteById(id);
    }
}
